package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.ReservationQueryDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.properties.JwtTokenProperties;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ThreadLocalUtil;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import com.zzyl.vo.VisitVo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ReservationServiceImpl implements ReservationService {

    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private JwtTokenProperties jwtTokenProperties;

    /**
     * 分页查找预约
     */
    @Override
    public PageBean<ReservationVo> findByPage(ReservationQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        Long userId = ThreadLocalUtil.get();
        dto.setUserId(userId);
        Page<Reservation> page = reservationMapper.findByPage(dto);
        return PageBean.of(page, ReservationVo.class);
    }

    @Override
    public Integer cancelledcount() {
        LocalDateTime withs = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime withe = LocalDateTime.now().with(LocalTime.MAX);
       /* ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String id = request.getHeader("Authorization");
        Claims claims = JwtUtil.parseJWT(jwtTokenProperties.getSecretKey(), id);
        Long userid = Long.valueOf(claims.get("id").toString());*/
        Long userid = ThreadLocalUtil.get();
        int Count = reservationMapper.getCancelledCount(withs, withe, userid);
        return Count;
    }

    @Override
    public List<TimeCountVo> countByTime(Long time) {
        LocalDateTime withs = LocalDateTimeUtil.of(time).with(LocalTime.MIN);
        LocalDateTime withe = LocalDateTimeUtil.of(time).with(LocalTime.MAX);
        List<TimeCountVo> timeCountVos = reservationMapper.countByTimeRange(withs, withe);
        return timeCountVos;
    }

    @Override
    public void add(Reservation reservation) {
        //reservation.setUpdateBy(20L);
        //ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        Long userid = ThreadLocalUtil.get();
        reservation.setUpdateBy(userid);
        reservation.setStatus(0);
        reservationMapper.insert(reservation);
    }

    @Override
    public void cancel(Long id) {
        Reservation byId = reservationMapper.findById(id);
        byId.setStatus(2);
        reservationMapper.update(byId);
    }

    @Override
    public PageBean<VisitVo> query(ReservationQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page<Reservation> byPage = reservationMapper.findByPage(dto);
        return PageBean.of(byPage,VisitVo.class);
    }


}

